package com.smashcrush.client.gwt.client.editor;


import com.google.gwt.gen2.table.client.CellRenderer;
import com.google.gwt.gen2.table.client.ColumnDefinition;
import com.google.gwt.gen2.table.client.DefaultRowRenderer;
import com.google.gwt.gen2.table.client.DefaultTableDefinition;
import com.google.gwt.gen2.table.client.FixedWidthGridBulkRenderer;
import com.google.gwt.gen2.table.client.PagingScrollTable;
import com.google.gwt.gen2.table.client.ScrollTable;
import com.google.gwt.gen2.table.client.TableDefinition;
import com.google.gwt.gen2.table.client.TableDefinition.AbstractCellView;
import com.google.gwt.gen2.table.event.client.RowSelectionHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.smashcrush.client.gwt.client.editor.ScriptColumnDefinition.Group;
import com.smashcrush.model.Script;

public class ScriptScrollTable extends Composite {

	PagingScrollTable<Script> table;

	public ScriptScrollTable() {
		table = createScrollTable();

		initWidget(table);
	}
	
	public void reset() {
		table.reloadPage();
		table.gotoFirstPage();
	}

	public void addRowSelectionHandler(RowSelectionHandler handler) {
		table.getDataTable().addRowSelectionHandler(handler);
	}
	
	public Script getSelected() {
		return table.getSelectedRowValues().iterator().next();
	}

	protected PagingScrollTable<Script> createScrollTable() {
		// Setup the controller
		ScriptTableModel cachedTableModel = new ScriptTableModel();
		cachedTableModel.setRowCount(1000);

		// Create a TableCellRenderer
		TableDefinition<Script> tableDef = createTableDefinition();

		// Create the scroll table
		PagingScrollTable<Script> pagingScrollTable = new PagingScrollTable<Script>(
				cachedTableModel, tableDef);

		// pagingScrollTable.setSortingEnabled(true);
		// pagingScrollTable
		// .setResizePolicy(ScrollTable.ResizePolicy.FLOW);
		pagingScrollTable.setSize("100%", "100%");

		pagingScrollTable.setPageSize(50);
		pagingScrollTable.setEmptyTableWidget(new HTML(
				"There is no data to display"));

		// Setup the bulk renderer
		FixedWidthGridBulkRenderer<Script> bulkRenderer = new FixedWidthGridBulkRenderer<Script>(
				pagingScrollTable.getDataTable(), pagingScrollTable);
		pagingScrollTable.setBulkRenderer(bulkRenderer);

		// Setup the formatting
		// pagingScrollTable.setCellPadding(3);
		// pagingScrollTable.setCellSpacing(0);
		pagingScrollTable.setResizePolicy(ScrollTable.ResizePolicy.FILL_WIDTH);
		pagingScrollTable.gotoFirstPage();

		return pagingScrollTable;
	}

	private TableDefinition<Script> createTableDefinition() {
		// Define some cell renderers
		CellRenderer<Script, Integer> intCellRenderer = new CellRenderer<Script, Integer>() {
			public void renderRowValue(Script rowValue,
					ColumnDefinition<Script, Integer> columnDef,
					AbstractCellView<Script> view) {
				view.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
				view.setHTML(columnDef.getCellValue(rowValue).toString());
			}
		};

		// Create the table definition
		DefaultTableDefinition tableDefinition = new DefaultTableDefinition<Script>();

		// Set the row renderer
		String[] rowColors = new String[] { "#FFFFDD", "#EEEEEE" };
		tableDefinition
				.setRowRenderer(new DefaultRowRenderer<Script>(rowColors));

		// First name
		{
			ScriptColumnDefinition<String> columnDef = new ScriptColumnDefinition<String>(
					"Name", Group.GENERAL) {
				@Override
				public String getCellValue(Script rowValue) {
					return rowValue.getName();
				}

				@Override
				public void setCellValue(Script rowValue, String cellValue) {
					rowValue.setName(cellValue);
				}
			};
			// columnDef.setMinimumColumnWidth(50);
			// columnDef.setPreferredColumnWidth(100);
			columnDef.setColumnSortable(true);
			columnDef.setColumnTruncatable(false);
			tableDefinition.addColumnDefinition(columnDef);
		}

		return tableDefinition;
	}

}
